<?php

class Benefit extends CActiveRecord
{
	public function tableName()
	{
		return 'benefits';
	}
	public function rules()
	{
		return array(
			array('title, type, disabled', 'required'),
			array('type, disabled', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>255),
			array('id, title, type, disabled', 'safe', 'on'=>'search'),
		);
	}

	public function relations()
	{
		return array(
		);
	}

	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Title',
			'type' => 'Type',
			'disabled' => 'Disabled',
		);
	}

	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('type',$this->type);
		$criteria->compare('disabled',$this->disabled);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function getAllByType($type){
		$criteria = new CDbCriteria;
		$criteria->condition = 'type=:type AND disabled=:status';
		$criteria->params = array(':type' => $type, ':status' => 1);
		$criteria->order = 'title ASC';
		return Benefit::model()->findAll($criteria);
	}
}
